package com.cdd.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.cdd.eduservice.entity.subject.OneSubject;
import com.cdd.eduservice.service.IEduSubjectService;
import com.cdd.eduservice.entity.EduSubject;
import com.cdd.eduservice.entity.excel.SubjectDate;
import com.cdd.eduservice.listener.SubjectExcelListener;
import com.cdd.eduservice.mapper.EduSubjectMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author cdd
 * @since 2021-02-01
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements IEduSubjectService {

    List<EduSubject> eduSubjects11 = null;

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file, IEduSubjectService iEduSubjectService) {
        try {
            // 文件输入流
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectDate.class,new SubjectExcelListener(iEduSubjectService)).sheet().doRead();

        }catch (Exception e){
            e.printStackTrace();
        }

    }

//    @Override
//    public List<OneSubject> getAllOneTwoSubject() {
//
//        // 获取一级分类数据
//        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
//        wrapperOne.eq("parent_id","0");
//        List<EduSubject> listOne = this.list(wrapperOne);
//
//        // 获取二级分类数据
//        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
//        wrapperTwo.ne("parent_id","0");
//        List<EduSubject> listTwo = this.list(wrapperTwo);
//
//        // 最终list结果
//        List<OneSubject> finalList = new ArrayList<>();
//
//        // 封装一级分类
//        for (int i = 0; i < listOne.size(); i++){
//            EduSubject eduSubject = listOne.get(i);
//            OneSubject oneSubject = new OneSubject();
//            BeanUtils.copyProperties(eduSubject, oneSubject);
//            finalList.add(oneSubject);
//
//            List<TwoSubject> twoFinalList = new ArrayList<>();
//
//            for (int j = 0; j < listTwo.size(); j++) {
//                EduSubject eduSubject1 = listTwo.get(j);
//                if(eduSubject1.getParentId().equals(eduSubject.getId())) {
//                    TwoSubject twoSubject = new TwoSubject();
//                    BeanUtils.copyProperties(eduSubject1, twoSubject);
//                    twoFinalList.add(twoSubject);
//                }
//            }
//            oneSubject.setChildren(twoFinalList);
//        }
//
//        // 封装二级分类
//
//        return finalList;
//    }


    // 递归实现的多级分类
    public List<OneSubject> Recursion(){
        // 获取所有数据
        List<EduSubject> eduSubjects = this.list();
        eduSubjects11 = eduSubjects;
        List<OneSubject> finalSubjectList = new ArrayList<>();
        for(EduSubject subject : eduSubjects11) {
            OneSubject parentSubject = new OneSubject();
            if(subject.getParentId().equals("0")) {
                BeanUtils.copyProperties(subject, parentSubject);
                parentSubject.setChildren(getChild(subject.getId()));
                finalSubjectList.add(parentSubject);
            }
        }
        return finalSubjectList;
    }

    public List<OneSubject> getChild(String id) {
        List<OneSubject> list = new ArrayList<>();
        for(EduSubject subject : eduSubjects11) {
            OneSubject childSubject = new OneSubject();
            if(subject.getParentId().equals(id)) {
                BeanUtils.copyProperties(subject, childSubject);
                childSubject.setChildren(getChild(subject.getId()));
                list.add(childSubject);
            }
        }
        return list;
    }
}
